import urlConfig from './config.js'
import {
	getToken,
	setToken
} from './auth.js'

const request = {}
const headers = {}

request.urlRequest = (objdata, load = 1) => {
	let token = getToken();
	// console.log(token)
	if (token != '') {
		headers['Authorization'] = "Bearer  " + token;
	}

	let reqUrl = urlConfig + objdata.url;

	
	// if (load > 0) {
	// 	uni.showLoading({
	// 		title: '加载中...',
	// 		mask: true
	// 	});
	// }
	return uni.request({
		url: reqUrl,
		method: objdata.method,
		data: objdata.data,
		dataType: 'json',
		header: headers
	}).then(res => {
		// setTimeout(function() {
		uni.hideLoading();
		// }, 1000);
		let rs = res[1].data;
		if (rs.code === 50008 || rs.code === 501) {
			setToken('');
			uni.navigateTo({
				url: '/pages2/pages/login/login'
			})
		} else if (rs.code == 400) {
			uni.showToast({
				title: rs.msg,
				icon: 'none'
			})
		}
		return rs;
	}).catch(parmas => {
		uni.hideLoading();
		switch (parmas.code) {
			case 401:
				uni.clearStorageSync()
				break;
			default:
				uni.showToast({
					title: parmas.msg,
					icon: 'none'
				})
				return Promise.reject()
				break;
		}
	})
}

export default request